using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pokemon
{
    class Freeze: Bullet
    {
        protected static List<Texture2D> _texture2Ds = new List<Texture2D>();

        protected static List<Vector2> _sizeTextures = new List<Vector2>();

        protected static List<int> _numFrame = new List<int>();

        protected static int _speed;

        protected static string _pathXML;//Chua duong dan den file dac ta XML

        protected bool _hit = false;

        public Freeze(Game game, Unit owner, Unit target) : base(game, owner, target)
        {
            _position = new Vector2(owner.Position.X + owner.Size.X, owner.Position.Y - 10);
        }
        public new static void Initialize()
        {
            XDocument doc = XDocument.Load(_pathXML);
            XName name = XName.Get(@"Speed");
            
            if (doc.Document != null)
            {
                var definitions = doc.Document.Descendants(name);
                _speed = Int32.Parse((definitions.First()).Value);

                name = XName.Get(@"Style");
                definitions = doc.Document.Descendants(name);
                foreach (var definition in definitions)
                {
                    var xAttribute = definition.Attribute(@"SheetName");
                    if (xAttribute != null)
                    {
                        _texture2Ds.Add(_game.Content.Load<Texture2D>(xAttribute.Value));
                        xAttribute = definition.Attribute(@"NumFrame");
                        if (xAttribute != null)
                        {
                            var value = Int32.Parse(xAttribute.Value);
                            _numFrame.Add(value);
                        }

                        xAttribute = definition.Attribute(@"FrameWidth");
                        if (xAttribute != null)
                        {
                            var width = Int32.Parse(xAttribute.Value);
                            xAttribute = definition.Attribute(@"FrameHeight");
                            if (xAttribute != null)
                            {
                                var height = Int32.Parse(xAttribute.Value);
                                _sizeTextures.Add(new Vector2(width, height));
                            }
                        }
                    }
                }
            }
        }

        public static void AddPath(string path)
        {
            _pathXML = path;
        }

        public void NextFrame()
        {
            _frameIndex = (_frameIndex + 1) % (_numFrame[0]);            
        }

        public override void Update(GameTime gameTime)
        {
            try
            {
                _timeSpan += gameTime.ElapsedGameTime;
                //if (_timeSpan.Milliseconds < 300)
                //    return;
                //_timeSpan = TimeSpan.Zero;

                base.Update(gameTime);

                _position.X += 1;
                NextFrame();

                //Neu chua trung dich thi chua chuyen sang frame hieu ung no
                if (_frameIndex >= 4 && !_hit)
                    _frameIndex = 3;
            }
            catch (Exception)
            {                
                throw;
            }            
        }

        public override void Draw(GameTime gameTime)
        {
            try
            {
                _timeSpan += gameTime.ElapsedGameTime;
                //if (_timeSpan.Milliseconds < 100)
                //    return;
                //_timeSpan = TimeSpan.Zero;

                _SpriteBatch.Draw(_texture2Ds[0], _position, GetNextRect(), Color.White);



                //Kiem tra xem enemy da bi loai khoi vong chien chua
                _target = _owner.Enemy;
                if (_target != null)
                {
                    Rectangle rectangle = new Rectangle((int)_position.X, (int)_position.Y,
                    (int)_sizeTextures[0].X / _numFrame[0],
                    (int)_sizeTextures[0].Y);

                    if (rectangle.Contains(_target.GetCenterPoint()) && !_hit)
                    {
                        _target.TakeDamage(_owner.Damage, Color.White);
                        _hit = true;
                    }
                }
                if (_frameIndex == _numFrame[0] - 1)
                {
                    BulletManager b = (BulletManager)Game.Services.GetService(typeof(BulletManager));
                    b.Remove(this);
                }
            }
            catch (Exception)
            {                
                throw;
            }
            
        }

        public Rectangle GetNextRect()
        {
            Rectangle rect;
            rect = new Rectangle(_frameIndex * (int)_sizeTextures[0].X / _numFrame[0], 0, (int)_sizeTextures[0].X / _numFrame[0], (int)_sizeTextures[0].Y);
            return rect;
        }
    }
}
